package m.jclecai.com.dialog;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;

/**
 * Created by Dinosa on 2018/1/2.
 * 合买中心的筛选对话框；
 */

public class GroupBuyScreenDialog extends BaseDialog {

    @BindView(R.id.cb_secrecy_state_all)
    CheckBox mCbSecrecyStateAll;

    @BindView(R.id.cb_secrecy_state_open)
    CheckBox mCbSecrecyStateOpen;

    @BindView(R.id.cb_secrecy_state_follow)
    CheckBox mCbSecrecyStateFollow;

    @BindView(R.id.cb_secrecy_state_close)
    CheckBox mCbSecrecyStateClose;

    @BindView(R.id.rb_progress_all)
    RadioButton mRbProgressAll;

    @BindView(R.id.rb_progress_can_not_join)
    RadioButton mRbProgressCanNotJoin;

    @BindView(R.id.rb_progress_can_join)
    RadioButton mRbProgressCanJoin;

    @BindView(R.id.rb_is_baodi_all)
    RadioButton mRbIsBaodiAll;

    @BindView(R.id.rb_is_baodi_yes)
    RadioButton mRbIsBaodiYes;

    @BindView(R.id.rb_is_baodi_no)
    RadioButton mRbIsBaodiNo;

    @BindView(R.id.tv_cancel)
    TextView mTvCancel;

    @BindView(R.id.tv_sure)
    TextView mTvSure;

    private  ArrayList<CheckBox> mCheckBoxes;
    private  ArrayList<RadioButton> mProgressList;
    private  ArrayList<RadioButton> mBaoDiList;

    @BindView(R.id.rg_progress)
    RadioGroup mRgProgress;

    @BindView(R.id.rg_baodi)
    RadioGroup mRgBaodi;

    /**
     * 构造方法，设置初始化参数，
     * @param context
     * @param onDialogClickListener  确定和取消的一个回掉的接口；
     * @param secrecy   保密状态
     * @param progress 进度状态
     * @param baodi  是否保底；
     */
    public GroupBuyScreenDialog(Context context,OnDialogClickListener onDialogClickListener,
                                String secrecy,String progress,String baodi) {
        super(context);
        setContentView(R.layout.layout_group_buy_screen);

        mOnDialogClickListener=onDialogClickListener;

        initSecrecyList();
        initProgressList();
        initBaoDiList();
        initData(secrecy,progress,baodi);
    }

    private void initData(String secrecy,String progress,String baodi) {
        setChecked(mProgressList,progress);
        setChecked(mBaoDiList,baodi);
        setChecked(mCheckBoxes,secrecy);
    }


    public void setChecked(List<? extends CompoundButton> list,String tag){
        if (list != null) {
            for (CompoundButton radioButton : list) {
                if (radioButton.getTag().toString().equals(tag)) {
                    radioButton.setChecked(true);
                }else{
                    radioButton.setChecked(false);
                }
            }
        }
    }

    private void initBaoDiList() {
        mBaoDiList=new ArrayList<>();
        mBaoDiList.add(mRbIsBaodiAll);
        mBaoDiList.add(mRbIsBaodiYes);
        mBaoDiList.add(mRbIsBaodiNo);
    }

    private void initProgressList() {
        mProgressList=new ArrayList<>();
        mProgressList.add(mRbProgressAll);
        mProgressList.add(mRbProgressCanJoin);
        mProgressList.add(mRbProgressCanNotJoin);
    }

    private void initSecrecyList() {
        mCheckBoxes = new ArrayList<>();
        mCheckBoxes.add(mCbSecrecyStateAll);
        mCheckBoxes.add(mCbSecrecyStateClose);
        mCheckBoxes.add(mCbSecrecyStateFollow);
        mCheckBoxes.add(mCbSecrecyStateOpen);
    }

    @OnClick(R.id.tv_cancel)
    public void OnClickCancel() {

        if (mOnDialogClickListener != null) {
            mOnDialogClickListener.onClick(false,"","","");
        }
        dismiss();
    }

    @OnClick(R.id.tv_sure)
    public void OnClickSure() {
        //这里我们将获取点击的内容；
        String secrecyCheckedBox = getSecrecyCheckedBox();
        String progressChecked = getProgressChecked();
        String baoDiChecked = getBaoDiChecked();
        System.out.println("screenDialog:secrecyCheckBox: "+secrecyCheckedBox);
        System.out.println("screenDialog:progressChecked: "+progressChecked);
        System.out.println("screenDialog:baoDiChecked: "+baoDiChecked);

        if (mOnDialogClickListener != null) {
            mOnDialogClickListener.onClick(true,secrecyCheckedBox,progressChecked,baoDiChecked);
        }
        dismiss();
    }

    @OnClick(R.id.cb_secrecy_state_close)
    public void OnClickClose() {
        onClickSecrecy(mCbSecrecyStateClose);
    }

    @OnClick(R.id.cb_secrecy_state_all)
    public void OnClickAll() {
        onClickSecrecy(mCbSecrecyStateAll);
    }

    @OnClick(R.id.cb_secrecy_state_follow)
    public void OnClickFollow() {
        onClickSecrecy(mCbSecrecyStateFollow);
    }

    @OnClick(R.id.cb_secrecy_state_open)
    public void OnClickOpen() {
        onClickSecrecy(mCbSecrecyStateOpen);
    }

    /**
     * 这里点击保密状态；
     */
    public void onClickSecrecy(CheckBox checkBox) {

        for (CheckBox box : mCheckBoxes) {
            if (box == checkBox) {
                checkBox.setChecked(true);
            } else {
                box.setChecked(false);
            }
        }
    }

    /**
     * 找到选中的保密模式；
     *
     * @return
     */
    public String getSecrecyCheckedBox() {

        for (CheckBox checkBox : mCheckBoxes) {
            if (checkBox.isChecked()) {
                return checkBox.getTag().toString();
            }
        }
        return "";
    }

    /**
     * 获取进度的选中项
     * @return
     */
    public String getProgressChecked() {
        RadioButton radioButton = (RadioButton) findViewById(mRgProgress.getCheckedRadioButtonId());
        return radioButton.getTag().toString();
    }

    /**
     * 获取保底的选中项
     * @return
     */
    public String getBaoDiChecked() {
        RadioButton radioButton = (RadioButton) findViewById(mRgBaodi.getCheckedRadioButtonId());
        return radioButton.getTag().toString();
    }

    public  OnDialogClickListener mOnDialogClickListener;


    public interface OnDialogClickListener{
        public void onClick(boolean isSure,String secrecy,String progress,String baoDi);
    }
}
